package com.gxa.p2p.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.VerifiedInfo;
import com.gxa.p2p.service.VerifiedInfoService;
import com.gxa.p2p.util.Authentic;
import com.gxa.p2p.util.FileUploadUtil;
import com.gxa.p2p.util.MapUtil;
import com.gxa.p2p.util.ReturnMap;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/verifiedInfo")
@Api(value = "实名认证的控制器", tags = "实名认证模块")
public class VerifiedInfoController {

    @Resource(name = "verifiedInfoService")
    VerifiedInfoService verifiedInfoService;

    /**
     * 查询数据库所有数据 可带条件查询
     *
     * @param paramMap
     * @return Map
     */
    @PostMapping
    @ResponseBody
    @ApiOperation(value = "查询所有数据的接口,可输入状态,时间范围这两个条件", response = Map.class, httpMethod = "POST")
    public Map<String, Object> findAll(@RequestBody @ApiParam(value = "参数map,包含状态,时间范围,分页参数", required = true, type = "Map") Map<String, Object> paramMap) {
        List<VerifiedInfo> list = verifiedInfoService.findAll(paramMap);
        Integer dataSum = verifiedInfoService.dataSum(paramMap);
        MapUtil mapUtil = new MapUtil();
        if (dataSum > 0) {
            mapUtil.put("data", list);
            mapUtil.put("count", dataSum);
            mapUtil.setCode(0);
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     * 追加审核信息(改) 添加到该条记录上
     *
     * @param verifiedInfo
     * @return Map
     */
    @PutMapping
    @ResponseBody
    @ApiOperation(value = "追加审核信息的接口", response = Map.class, httpMethod = "PUT")
    public Map<String, Object> updateReviewStatus(@RequestBody @ApiParam(value = "verifiedInfo对象,包含该条实名认证的所有信息", required = true, type = "VerifiedInfo") VerifiedInfo verifiedInfo) {
        Integer lines = verifiedInfoService.updateReviewStatus(verifiedInfo);
        MapUtil mapUtil = new MapUtil();
        if (lines > 0)
            mapUtil.setCode(0);
        else
            throw new BusinessException(ErrorCode.FAILED);
        return mapUtil.getMap();
    }

    // 添加信息
    @PostMapping("/saveVer")
    @ResponseBody
    @ApiOperation(value = "保存审核信息的接口", response = VerifiedInfo.class, httpMethod = "Post")
    public Map<String, Object> saveVer(@RequestBody VerifiedInfo verifiedInfo) throws TencentCloudSDKException {
        System.out.println(verifiedInfo);
        JSONObject jsonObject = Authentic.authenticIDCard(verifiedInfo.getIdCard(), verifiedInfo.getRealName());
        String result = (String) jsonObject.get("Result");
        MapUtil mapUtil = new MapUtil();
        if ("0".equals(result) && verifiedInfoService.saveVer(verifiedInfo)) { //上线用
//        if (verifiedInfoService.saveVer(verifiedInfo)) { //测试用
            mapUtil.setCode(0);
            mapUtil.setMsg("添加成功！！");
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    @PostMapping("/ImgUpload")
    @ResponseBody
    @ApiOperation(value = "身份证图片上传接口", response = List.class, httpMethod = "Post")
    public Map<String, Object> upLoad(@RequestParam("file") List<MultipartFile> multipartFileList, HttpServletRequest httpServletRequest) throws IOException {
        List<Map<String, Object>> upload = FileUploadUtil.upload(multipartFileList, httpServletRequest);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(upload);
    }


    @GetMapping("findUserInfo")
    @ResponseBody
    @ApiOperation(value = "查询单个用户的认证信息", tags = "查询单个用户的认证信息")
    public Map<String, Object> findUserInfo(@RequestParam("userId") Integer userId) {
        VerifiedInfo userInfo = verifiedInfoService.findUserInfo(userId);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(userInfo);
    }
}
